﻿using Capstone.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Capstone.Models;
using Capstone.Services;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using Capstone.Views;
using System.Net.Http;

namespace Capstone.Views
{
    public partial class StoolQualityDiaryPage : ContentPage
    {

        public List<int> DateOptions1;
       // public List<string> FrequencyOptions1;
        public List<string> SymptomOptions1;

        public StoolQualityDiaryPage()
        {
            InitializeComponent();
            BindingContext = new StoolQualityDiaryViewModel();
            DateOptions1 = AddDateOptions();
            //FrequencyOptions1 = AddDiaryFrequency();
            SymptomOptions1 = AddSymptoms();
            SymptomList.ItemsSource = SymptomOptions1;
            SymptomList.SelectedIndices = new List<int> { };
            DiaryLength.ItemsSource = DateOptions1;
      //      DiaryFrequency1.ItemsSource = FrequencyOptions1;
        }
        List<int> AddDateOptions()
        {
            List<int> options = new List<int>();
            for (int i = 1; i < 366; i++)
            {
                options.Add(i);
            }
            return options;
        }
        List<string> AddSymptoms()
        {
            List<string> options = new List<string>();
            options.Add(Properties.Resources.StoolQuestions1);
            options.Add(Properties.Resources.StoolQuestions2);
            options.Add(Properties.Resources.StoolQuestions3);
            options.Add(Properties.Resources.StoolQuestions4);
            options.Add(Properties.Resources.StoolQuestions5);
            options.Add(Properties.Resources.StoolQuestions6);
            //  options.Add("Have any addition information");
            return options;
        }
        private async void SaveFunction(object sender, EventArgs e)
        {
            //if (DiaryName1.Text == null || DiaryName1.Text == "" || DiaryLength1.SelectedIndex == -1 || (string.IsNullOrWhiteSpace(SymptomList1.Text)))
            if (DiaryName.Text == null || DiaryName.Text == "" || DiaryLength.SelectedIndex == -1)
                {
                ErrorMessage1.IsVisible = true;
            }
            else
            {
                Item newItem = new Item()
                {
                    Id = Guid.NewGuid().ToString(),
                    Text = DiaryName.Text,
                    Description = Properties.Resources.StoolDescription,
                    Length = DiaryLength.SelectedIndex + 1,
                    Symptoms = SymptomList.Text.Split(new char[] { '\u002C' }, StringSplitOptions.RemoveEmptyEntries),
                    startDate = startDateX
                    //Frequency = (string)DiaryFrequency1.SelectedItem,
                    //Text1 = AdditionInformation.Text
                };

                int customInt = 0;
                HttpClient client = new HttpClient();

                string data = "[{\"record_id\":\"" + DiaryName.Text + " (ID:" + GetHashCode() + ")" + "\", \"stool_quality_study_name\":\"" + DiaryName.Text + "\"";
                data += ", \"stool_diary_time\":\"" + startDateX.Date.Year + "-" + startDateX.Date.Month + "-" + startDateX.Date.Day + "\"";
                data += ", \"stool_quality_days\":\"" + (DiaryLength.SelectedIndex + 1) + "\"";

                string symptoms = SymptomList.Text;

                if (symptoms.Contains(Properties.Resources.StoolQuestions1))
                {
                    data += ", \"stool_quality_questions___1\":\"" + "1" + "\"";
                    customInt++;
                }
                else data += ", \"stool_quality_questions___1\":\"" + "0" + "\"";
                if (symptoms.Contains(Properties.Resources.StoolQuestions2))
                {
                    data += ", \"stool_quality_questions___2\":\"" + "1" + "\"";

                    customInt++;
                }
                else data += ", \"stool_quality_questions___2\":\"" + "0" + "\"";
                if (symptoms.Contains(Properties.Resources.StoolQuestions3))
                {
                    data += ", \"stool_quality_questions___3\":\"" + "1" + "\"";

                    customInt++;
                }
                else data += ", \"stool_quality_questions___3\":\"" + "0" + "\"";
                if (symptoms.Contains(Properties.Resources.StoolQuestions4))
                {
                    data += ", \"stool_quality_questions___4\":\"" + "1" + "\"";

                    customInt++;
                }
                else data += ", \"stool_quality_questions___4\":\"" + "0" + "\"";
                if (symptoms.Contains(Properties.Resources.StoolQuestions5))
                {
                    data += ", \"stool_quality_questions___5\":\"" + "1" + "\"";

                    customInt++;
                }
                else data += ", \"stool_quality_questions___5\":\"" + "0" + "\"";
                if (symptoms.Contains(Properties.Resources.StoolQuestions6))
                {
                    data += ", \"stool_quality_questions___6\":\"" + "1" + "\"";

                    customInt++;
                }
                else data += ", \"stool_quality_questions___6\":\"" + "0" + "\"";


                if (newItem.Symptoms.Length > customInt)
                {
                    if (newItem.Symptoms.Length - customInt == 1)
                    {

                        data += ", \"stool_quality_questions___7\":\"" + "1" + "\"";
                    }

                    if (newItem.Symptoms.Length - customInt == 2)
                    {
                        data += ", \"stool_quality_questions___7\":\"" + "1" + "\"";
                        data += ", \"stool_quality_questions___8\":\"" + "1" + "\"";
                    }

                    if (newItem.Symptoms.Length - customInt == 3)
                    {
                        data += ", \"stool_quality_questions___7\":\"" + "1" + "\"";
                        data += ", \"stool_quality_questions___8\":\"" + "1" + "\"";
                        data += ", \"stool_quality_questions___9\":\"" + "1" + "\"";
                    }

                    if (newItem.Symptoms.Length - customInt == 4)
                    {
                        data += ", \"stool_quality_questions___7\":\"" + "1" + "\"";
                        data += ", \"stool_quality_questions___8\":\"" + "1" + "\"";
                        data += ", \"stool_quality_questions___9\":\"" + "1" + "\"";
                        data += ", \"stool_quality_questions___10\":\"" + "1" + "\"";
                    }
                }

                data += ", \"part_emails_stool\":\"" + part_emails.Text + "\"";

                data += "}]";

                var values = new Dictionary<string, string>
                {
                    { "token", "540E97741AA3EFF280E55B5C9DB1CD6D" },
                    { "content", "record" },
                    { "action", "import" },
                    { "format", "json" },
                    { "overwriteBehavior", "normal" },
                    { "forceAutoNumber", "false" },
                    { "data",  data},
                    { "returnContent", "count" },
                    {"returnFormat", "json" }
                };

                var content = new FormUrlEncodedContent(values);
                var response = await client.PostAsync("https://redcap.utu.fi/api/", content);
                var responseString = await response.Content.ReadAsStringAsync();
                Console.WriteLine(responseString);
                if (responseString.Contains("error"))
                {
                    Save_Fail.IsVisible = true;
                    Save_Success.IsVisible = false;
                }
                else
                {
                    Save_Success.IsVisible = true;
                    Save_Fail.IsVisible = false;
                }

                Console.WriteLine(data);

                await DependencyService.Get<IDataStore<Item>>().AddItemAsync(newItem);
                await Navigation.PopAsync();
                await Navigation.PopAsync();
            }
        }
    }
}